package com.rural.bus.view;

import java.util.Arrays;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rural.bus.R;

/**
 * 
 * @author Peanut
 */
public class SlideItemDialog extends Dialog implements View.OnClickListener {
	private Button btn_confirm, btn_cancel;
	private WheelView main_wv;
	private String[] datas;
	private OnDialogDismissListener listener;
	int index;
	
	private TextView mTvTitle;
	private String title;

	public interface OnDialogDismissListener {
		void onConfirmClicked(int index);

		void onCancelClicked();
	}

	public SlideItemDialog(Context context,String title, String[] datas, int index, OnDialogDismissListener listener) {
		super(context, R.style.common_alert_dialog);
		this.title=title;
		this.datas = datas;
		this.index = index;
		this.listener = listener;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_slide_item_dialog);
		initView();
	}

	private void initView() {
		main_wv = (WheelView) findViewById(R.id.main_wv);
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		btn_confirm.setOnClickListener(this);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(this);
		mTvTitle=(TextView) findViewById(R.id.tv_dialog_title);
		mTvTitle.setText(title);
		
		main_wv.setOffset(1);  
		main_wv.setSeletion(index);//设置默认选择项
		main_wv.setItems(Arrays.asList(datas));
		main_wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, String item) {
				Log.d("driver", "selectedIndex: " + selectedIndex + ", item: " + item);
				index = selectedIndex -1;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_confirm:
			if (listener != null) {
				dismiss();
				listener.onConfirmClicked(index);
			}
			break;
		case R.id.btn_cancel:
			if (listener != null) {
				dismiss();
			}
			break;
		default:
			break;
		}
	}
	
//	public void setDialogTitle(String title){
//		mTvTitle.setText(title);
//	}
	
}
